package com.guducat.collegeWeb.entity;

import lombok.Data;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 用户数据实体类
 * 对应 user_data 表
 */
@Data
public class UserData {

    /**
     * 主键ID
     */
    private Long id;

    /**
     * 用户ID，关联user表
     */
    private Integer userId;

    /**
     * 数据类型：favorite(收藏)、history(历史)、like(点赞)
     */
    private String dataType;

    /**
     * 文章ID，关联news表的news_id字段
     */
    private Integer newsId;

    /**
     * 文章标题（冗余存储，便于查询）
     */
    private String newsTitle;

    /**
     * 文章URL（冗余存储）
     */
    private String newsUrl;

    /**
     * 文章分类（冗余存储）
     */
    private String newsCategory;

    /**
     * 文章所属行政级别（冗余存储）
     */
    private String newsSection;

    /**
     * 扩展数据，存储额外信息如阅读时长、收藏时的标签等
     * 在MyBatis中会自动转换为JSON字符串存储
     */
    private Map<String, Object> extraData;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 数据类型常量
     */
    public static class DataType {
        public static final String FAVORITE = "favorite";
        public static final String HISTORY = "history";
        public static final String LIKE = "like";
    }

    /**
     * 构造方法
     */
    public UserData() {}

    public UserData(Integer userId, String dataType, Integer newsId) {
        this.userId = userId;
        this.dataType = dataType;
        this.newsId = newsId;
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    public UserData(Integer userId, String dataType, Integer newsId, String newsTitle, String newsUrl) {
        this(userId, dataType, newsId);
        this.newsTitle = newsTitle;
        this.newsUrl = newsUrl;
    }

    public UserData(Integer userId, String dataType, Integer newsId, String newsTitle, String newsUrl,
                   String newsCategory, String newsSection) {
        this(userId, dataType, newsId, newsTitle, newsUrl);
        this.newsCategory = newsCategory;
        this.newsSection = newsSection;
    }

    public UserData(Integer userId, String dataType, Integer newsId, String newsTitle, String newsUrl,
                   String newsCategory, String newsSection, Map<String, Object> extraData) {
        this(userId, dataType, newsId, newsTitle, newsUrl, newsCategory, newsSection);
        this.extraData = extraData;
    }

    /**
     * 检查是否为收藏数据
     */
    public boolean isFavorite() {
        return DataType.FAVORITE.equals(this.dataType);
    }

    /**
     * 检查是否为历史数据
     */
    public boolean isHistory() {
        return DataType.HISTORY.equals(this.dataType);
    }

    /**
     * 检查是否为点赞数据
     */
    public boolean isLike() {
        return DataType.LIKE.equals(this.dataType);
    }

    /**
     * 更新时间戳
     */
    public void updateTimestamp() {
        this.updateTime = LocalDateTime.now();
    }

    @Override
    public String toString() {
        return "UserData{" +
                "id=" + id +
                ", userId=" + userId +
                ", dataType='" + dataType + '\'' +
                ", newsId=" + newsId +
                ", newsTitle='" + newsTitle + '\'' +
                ", newsUrl='" + newsUrl + '\'' +
                ", newsCategory='" + newsCategory + '\'' +
                ", newsSection='" + newsSection + '\'' +
                ", extraData=" + extraData +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                '}';
    }
}
